package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

import android.util.Log;
import android.widget.ListView;

public class DistrictLoader {
	JSONParser jParser = new JSONParser(); 
    
	ListView list;
	
	private static String url_get_district_by_province_id = IP.get_IP() +  IP.get_path_service() + "get_district_by_province_id.php";
	//private static String url_get_district_by_province_id = IP.get_IP() +  "/zigo/KLTN/WebservicePHP/get_district.php";
	
	// JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_DISTRICT = "district";
    public final String TAG_DISTRICT_ID = "id";
    public final String TAG_DISTRICT_NAME = "name";
    public final String TAG_PROVINCE_ID = "province_id";

	String province_id;
	
    // products JSONArray
    JSONArray districts = null;
    
    public DistrictLoader(String ProvinceID)
    {
    	province_id = ProvinceID;
    }
    
    public ArrayList<HashMap<String, String>> GetDistrict()
    {
    	ArrayList<HashMap<String, String>> districtsList = new ArrayList<HashMap<String,String>>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("province_id", province_id));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_get_district_by_province_id, "POST", params);

        Log.d("All Districts: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
            	districts = json.getJSONArray(TAG_DISTRICT);

                // looping through All Products
                for (int i = 0; i < districts.length(); i++) {
                    JSONObject c = districts.getJSONObject(i);

                    // Storing each json item in variable
                    String districtId = c.getString(TAG_DISTRICT_ID);
                    String districtName = c.getString(TAG_DISTRICT_NAME);
                    String provinceID = province_id; //c.getString(TAG_PROVINCE_ID);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_DISTRICT_ID, districtId);
                    map.put(TAG_DISTRICT_NAME, districtName);
                    map.put(TAG_PROVINCE_ID, provinceID);

                    // adding HashList to ArrayList
                    districtsList.add(map);
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return districtsList;
    }
}
